/*
 * @Description: 
 * @FilePath: /STM32F103/USER/bsp/dma.c
 * @Author: Wei Zhou
 * @Github: https://github.com/zromyk
 * @Date: 2021-05-12 20:17:46
 * @LastEditors: Wei Zhou
 * @LastEditTime: 2021-05-13 00:19:27
 * @Copyright: Copyright © 2017 muyiro. All rights reserved.
 */

#include "dma.h"
#include "nvic.h"
#include "rcc.h"

void dma_tx_init(DMA_Dev *dma, uint32_t DR_addr, uint8_t *tx_buf, uint32_t tx_buf_size)
{
    DMA_InitTypeDef DMA_InitStruct;
    
    rcc_periph_clock_enable(dma->DMAx, dma->RCC_Periph_DMA);

    DMA_DeInit(dma->tx_channel);

    DMA_InitStruct.DMA_DIR                   = DMA_DIR_PeripheralDST;
    DMA_InitStruct.DMA_Mode                  = DMA_Mode_Normal;             /* mode */
    DMA_InitStruct.DMA_PeripheralBaseAddr    = DR_addr;                     /* peripheral_address, address of DR, not value of DR */
    DMA_InitStruct.DMA_PeripheralInc         = DMA_PeripheralInc_Disable;
    DMA_InitStruct.DMA_PeripheralDataSize    = DMA_PeripheralDataSize_Byte;
    DMA_InitStruct.DMA_MemoryBaseAddr        = (uint32_t)tx_buf;            /* memory_address */
    DMA_InitStruct.DMA_BufferSize            = tx_buf_size;                 /* memory_size */
    DMA_InitStruct.DMA_MemoryInc             = DMA_MemoryInc_Enable; 
    DMA_InitStruct.DMA_MemoryDataSize        = DMA_MemoryDataSize_Byte;  
    DMA_InitStruct.DMA_Priority              = DMA_Priority_Medium;
    DMA_InitStruct.DMA_M2M                   = DMA_M2M_Disable;
    
    DMA_Init(dma->tx_channel, &DMA_InitStruct);
}

void dma_rx_init(DMA_Dev *dma, uint32_t DR_addr, uint8_t *rx_buf, uint32_t rx_buf_size)
{
    DMA_InitTypeDef DMA_InitStruct;
    
    rcc_periph_clock_enable(dma->DMAx, dma->RCC_Periph_DMA);
    
    DMA_DeInit(dma->rx_channel);

    DMA_InitStruct.DMA_DIR                   = DMA_DIR_PeripheralSRC;
    DMA_InitStruct.DMA_Mode                  = DMA_Mode_Normal;             /* mode */
    DMA_InitStruct.DMA_PeripheralBaseAddr    = DR_addr;                     /* peripheral_address, address of DR, not value of DR */
    DMA_InitStruct.DMA_PeripheralInc         = DMA_PeripheralInc_Disable;
    DMA_InitStruct.DMA_PeripheralDataSize    = DMA_PeripheralDataSize_Byte;
    DMA_InitStruct.DMA_MemoryBaseAddr        = (uint32_t)rx_buf;            /* memory_address */
    DMA_InitStruct.DMA_BufferSize            = rx_buf_size;                 /* memory_size */
    DMA_InitStruct.DMA_MemoryInc             = DMA_MemoryInc_Enable; 
    DMA_InitStruct.DMA_MemoryDataSize        = DMA_MemoryDataSize_Byte;  
    DMA_InitStruct.DMA_Priority              = DMA_Priority_VeryHigh;
    DMA_InitStruct.DMA_M2M                   = DMA_M2M_Disable;
    
    DMA_Init(dma->rx_channel, &DMA_InitStruct);
}
